package com.ruoyi.customer.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.customer.domain.CsOrder;
import com.ruoyi.customer.service.ICsOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 订单Controller
 * 
 * @author 李守壮
 * @date 2023-03-18
 */
@RestController
@RequestMapping("/customer/order")
public class CsOrderController extends BaseController
{
    @Autowired
    private ICsOrderService csOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('customer:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsOrder csOrder)
    {
        startPage();
        List<CsOrder> list = csOrderService.selectCsOrderList(csOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('customer:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsOrder csOrder)
    {
        List<CsOrder> list = csOrderService.selectCsOrderList(csOrder);
        ExcelUtil<CsOrder> util = new ExcelUtil<CsOrder>(CsOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(csOrderService.selectCsOrderById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('customer:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsOrder csOrder)
    {
        return toAjax(csOrderService.insertCsOrder(csOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('customer:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsOrder csOrder)
    {
        return toAjax(csOrderService.updateCsOrder(csOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('customer:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(csOrderService.deleteCsOrderByIds(ids));
    }


    /**
     * 导入
     */
    @Log(title = "CsOrder", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('customer:order:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CsOrder> util = new ExcelUtil<CsOrder>(CsOrder.class);
        List<CsOrder> list = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = csOrderService.importData(list, updateSupport, operName);
        return success(message);
    }
    /**
     * 导入模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CsOrder> util = new ExcelUtil<CsOrder>(CsOrder.class);
        util.importTemplateExcel(response, "数据");
    }

}
